package com.battery.system.service.battery.impl;

import com.battery.common.core.text.Convert;
import com.battery.common.enums.AccountTypeEnum;
import com.battery.common.enums.YesOrNoEnum;
import com.battery.common.utils.DateUtils;
import com.battery.system.domain.*;
import com.battery.system.domain.BO.MerchantBO;
import com.battery.system.domain.VO.MerchantExcelVO;
import com.battery.system.domain.VO.MerchantVO;
import com.battery.system.mapper.IncomeMapper;
import com.battery.system.mapper.MerchantInstallMapper;
import com.battery.system.mapper.MerchantMapper;
import com.battery.system.mapper.ProxyUserMapper;
import com.battery.system.service.battery.IMerchantService;
import com.battery.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商户Service业务层处理
 *
 * @author battery
 * @date 2021-01-22
 */
@Service
public class MerchantServiceImpl extends BaseServiceImpl<Merchant> implements IMerchantService {
    @Autowired
    private MerchantMapper merchantMapper;


    @Autowired
    private MerchantInstallMapper merchantInstallMapper;


    @Autowired
    private ProxyUserMapper proxyUserMapper;


    @Autowired
    private IncomeMapper incomeMapper;

    /**
     * 查询商户
     *
     * @param id 商户ID
     * @return 商户
     */
    @Override
    public Merchant selectMerchantById(String id) {
        return merchantMapper.selectMerchantById(id);
    }

    /**
     * 查询商户列表
     *
     * @param merchant 商户
     * @return 商户
     */
    @Override
    public List<Merchant> selectMerchantList(Merchant merchant) {
        return merchantMapper.selectMerchantList(merchant);
    }

    /**
     * 新增商户
     *
     * @param merchant 商户
     * @return 结果
     */
    @Override
    public int insertMerchant(Merchant merchant) {
        return merchantMapper.insertMerchant(merchant);
    }

    /**
     * 修改商户
     *
     * @param merchant 商户
     * @return 结果
     */
    @Override
    public int updateMerchant(Merchant merchant) {
        merchant.setUpdateTime(DateUtils.getNowDate());
        return merchantMapper.updateMerchant(merchant);
    }

    /**
     * 删除商户对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMerchantByIds(String ids) {
        return merchantMapper.deleteMerchantByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商户信息
     *
     * @param id 商户ID
     * @return 结果
     */
    @Override
    public int deleteMerchantById(String id) {
        return merchantMapper.deleteMerchantById(id);
    }

    /**
     * @param merchantBO
     * @return
     */
    @Override
    public List<MerchantVO> selectMerchantVOList(MerchantBO merchantBO) {
        return merchantMapper.selectMerchantVOList(merchantBO);
    }

    @Override
    public MerchantVO selectMerchantByphone(String phone) {
        return merchantMapper.selectMerchantByphone(phone);
    }

    @Override
    public MerchantStatistics selectMerchantStatistics(String id) {
        MerchantStatistics merchantStatistics = merchantMapper.selectMerchantStatistics(id);
        List<MerchantStoreList> list = merchantMapper.selectMerchantStoreList(id);
        merchantStatistics.setList(list);
        return merchantStatistics;
    }

    @Override
    public MerchantIncome selectMerchantByIncome(Map<String, String> map) {
//        MerchantIncome merchantIncome = merchantMapper.selectMerchantByIncome(map);
        MerchantIncome merchantIncome = merchantMapper.selectBorrowData(map); // 获取商户的租借笔数，以及退款金额
        if (merchantIncome != null) {
            BigDecimal merchantIncomeSum = incomeMapper.merchantIncomeSumById(map); // 查询商户的收益总和
            merchantIncome.setIncome(merchantIncomeSum);

            List<MerchantIncomeList> list = merchantMapper.selectMerchantIncomeList(map);
            merchantIncome.setList(list);
        }
        return merchantIncome;
    }

    /**
     * 查询该合作商的直营商户数量
     *
     * @param partnerId
     * @return
     */
    @Override
    public int selectDirectCountByPartnerId(String partnerId) {
        Example example = new Example(Merchant.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("partnerId", partnerId);
        criteria.andEqualTo("isDelete", 0);
        return merchantMapper.selectCountByExample(example);
    }

    /**
     * 逻辑删除商户
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public int deleteLogic(String id) {

        Date date = new Date();

        Merchant merchantUpdate = new Merchant();
        merchantUpdate.setId(id);
        merchantUpdate.setIsDelete(YesOrNoEnum.YES.getCode());
        merchantUpdate.setUpdateTime(date);
        merchantMapper.updateByPrimaryKeySelective(merchantUpdate);

        MerchantInstall installUpdate = new MerchantInstall();
        installUpdate.setMerchantId(id);
        installUpdate.setIsDelete(YesOrNoEnum.YES.getCode());
        merchantInstallMapper.updateByPrimaryKeySelective(installUpdate);

        // 删除对应账户信息
        ProxyUser proxyUserUpdate = new ProxyUser();
        proxyUserUpdate.setIsDeleted(YesOrNoEnum.NO.getCode());
        proxyUserUpdate.setUpdateTime(date);
        Example example = new Example(ProxyUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("accountType", AccountTypeEnum.MERCHANT.getCode());
        criteria.andEqualTo("accountId", id);
        return proxyUserMapper.updateByExampleSelective(proxyUserUpdate, example);
    }

    /**
     * 导出商户-门店信息
     *
     * @param merchantBO
     * @return
     */
    @Override
    public List<MerchantExcelVO> selectMerchantExcelVOList(MerchantBO merchantBO) {
        return merchantMapper.selectMerchantExcelVOList(merchantBO);
    }
}
